package com.sgb.kjwl.view.ui.activity.enterprise;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityServiceProjectclassListBinding;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.DialogHelper;
import com.sgb.kjwl.utils.DialogTextReturnValue;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceProjectClassListAdpater;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceActivityModel;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceProjectClassListViewModel;
import com.swgk.core.helper.OnMultiClickListener;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PublishServiceProjectClassListActivity extends AppActivity {

    private PublishServiceProjectClassListViewModel mViewModel;
    private ActivityServiceProjectclassListBinding mBinding;
    private ServiceProjectClassListAdpater mAdpater;


    @Override
    protected void initView() {
        super.initView();

        mViewModel = new ViewModelProvider(this).get(PublishServiceProjectClassListViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_service_projectclass_list);
        mBinding.setVariable(BR.model, mViewModel);
        mBinding.setLifecycleOwner(this);

        mViewModel.listdata = (List<ServiceProjectClassEntity>)getIntent().getSerializableExtra("data");
        initRecyclerView();
        showSaveBtn();
        initLister();
    }


    private void initRecyclerView(){
        mAdpater = new ServiceProjectClassListAdpater(this,mViewModel.listdata);
        mBinding.recyclerViewTrand.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewTrand.setAdapter(mAdpater);

        mAdpater.setLister(new ServiceProjectClassListAdpater.ClickLister() {
            @Override
            public void onclick(View view, int options_1, int options_2) {
                switch (view.getId()){
                    case R.id.image_delete://删除父项
                        com.sgb.kjwl.utils.DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, "温馨提示", "确认移除该分类？", "确定", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                 mViewModel.listdata.remove(options_1);
                                showSaveBtn();
                                mAdpater.notifyDataSetChanged();
                                mViewModel.isUpdate = true;
                            }
                        });
                        break;
                    case R.id.image_update://更新父项
                        DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, new DialogTextReturnValue() {
                            @Override
                            public void returnvalue(String value, Dialog dialog) {
                                if(mViewModel.listdata.size()>0){
                                    for (ServiceProjectClassEntity listdatum : mViewModel.listdata) {
                                        if(value.equals(listdatum.getName())){
                                            MToast.showLongToast(PublishServiceProjectClassListActivity.this,"分类名称不能重复");
                                            return;
                                        }
                                    }
                                }
                                dialog.dismiss();
                                mViewModel.listdata.get(options_1).setName(value);
                                mAdpater.notifyDataSetChanged();
//                                mViewModel.isUpdate = true;

                            }
                        },"分类重命名",mViewModel.listdata.get(options_1).getName(),"分类名称","请输入分类名称",true,20);
                        break;
                    case R.id.ll_add://添加子项
                        if(mViewModel.listdata.get(options_1).getChild().size()>=5){
                            MToast.showLongToast(PublishServiceProjectClassListActivity.this,"最多添加5项");
                            return;
                        }
                        DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, new DialogTextReturnValue() {
                            @Override
                            public void returnvalue(String value, Dialog dialog) {


                                if(mViewModel.listdata.get(options_1).AddChildBean(value)){
                                    dialog.dismiss();
                                    mAdpater.notifyDataSetChanged();
                                    mViewModel.isUpdate = true;
                                }else{
                                    MToast.showLongToast(PublishServiceProjectClassListActivity.this,"选项名称不能重复");
                                }


                            }
                        },"添加项目选项","","选项名称","请输入选项名称",true,20);
                        break;
                    case R.id.image_delete_label://删除子项
                        com.sgb.kjwl.utils.DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, "温馨提示", "确认移除该选项？", "确定", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                mViewModel.listdata.get(options_1).getChild().remove(options_2);
                                mAdpater.notifyDataSetChanged();
                                mViewModel.isUpdate = true;
                            }
                        });
                        break;
                    case R.id.image_update_label://更新子项
                        DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, new DialogTextReturnValue() {
                            @Override
                            public void returnvalue(String value, Dialog dialog) {


                                if(mViewModel.listdata.get(options_1).upDateChildBean(options_2,value)){
                                    dialog.dismiss();
                                    mAdpater.notifyDataSetChanged();
                                    mViewModel.isUpdate = true;
                                }else{
                                    MToast.showLongToast(PublishServiceProjectClassListActivity.this,"选项名称不能重复");
                                }


                            }
                        },"添加项目选项", mViewModel.listdata.get(options_1).getChild().get(options_2).getName(),"选项名称","请输入选项名称",true,20);

                        break;

                }
            }
        });
    }


    private void showSaveBtn(){
        mBinding.tvSaveBtn.setVisibility(mViewModel.listdata.size()>0?View.VISIBLE:View.GONE);
        mBinding.recyclerViewTrand.setVisibility(mViewModel.listdata.size()>0?View.VISIBLE:View.GONE);
        mBinding.include.setVisibility(mViewModel.listdata.size()>0?View.GONE:View.VISIBLE);
    }



    private void initLister(){
        mBinding.actionBar.setMenuFunctionClick(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if(mViewModel.listdata.size()>=5){
                    MToast.showLongToast(PublishServiceProjectClassListActivity.this,"最多添加5项");
                    return;
                }
                DialogHelper.showDialog(PublishServiceProjectClassListActivity.this, new DialogTextReturnValue() {
                    @Override
                    public void returnvalue(String value, Dialog dialog) {
                        if(mViewModel.listdata.size()>0){
                            for (ServiceProjectClassEntity listdatum : mViewModel.listdata) {
                                if(value.equals(listdatum.getName())){
                                    MToast.showLongToast(PublishServiceProjectClassListActivity.this,"分类名称不能重复");
                                    return;
                                }
                            }
                        }
                        dialog.dismiss();
                        mViewModel.listdata.add(new ServiceProjectClassEntity(value));
                        showSaveBtn();
                        mViewModel.isUpdate = true;
                        mAdpater.notifyDataSetChanged();

                    }
                },"添加项目分类","","分类名称","请输入分类名称",true,20);
            }
        });

        mBinding.tvSaveBtn.setOnClickListener(view ->{
            for (ServiceProjectClassEntity listdatum : mViewModel.listdata) {
                if(listdatum.getChild()==null||listdatum.getChild().size()==0){
                    MToast.showLongToast(this,"请至少添加1个选项");
                    return;
                }
            }
            Intent mIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("data",(Serializable)mViewModel.listdata);
            bundle.putBoolean("isupdate",mViewModel.isUpdate);
            mIntent.putExtras(bundle);
            setResult(PublishServiceActivity.RESULT_OK,mIntent);
            finish();
        });
    }



    public static void startactivity(Context mContext, List<ServiceProjectClassEntity> listdata){
        if(listdata==null){
            listdata = new ArrayList<>();
        }
        Intent mIntent = new Intent(mContext,PublishServiceProjectClassListActivity.class);
        mIntent.putExtra("data",(Serializable)listdata);
        mContext.startActivity(mIntent);


    }


}
